/**
 * 
 */
package com.personal.Visualplan.controller;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.personal.Visualplan.entity.Plan;
import com.personal.Visualplan.service.PlanService;
import com.personal.Visualplan.service.TagService;
import com.personal.Visualplan.service.UserService;

/**
 * <p> Project name:Visualplan </p>
 * <p> Class name:PlanOperatController </p>
 * @author "JiHongru"
 * @version
 * @date 2020年3月2日下午8:02:46
 * @description 该类为操作任务的Controller，包括发布任务、开始任务、完成任务
 *
 */
@RestController
@RequestMapping("/plan")
public class PlanOperatController {
	
	@Resource
	private PlanService planService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private TagService tagService;
	
	// 时间格式
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	
	/**
	 * 发布任务URL
	 * method = post
	 * @return String
	 *
	 */
	@RequestMapping(value = "/release", method = RequestMethod.POST)
	public Plan releasePlan(@RequestParam("content")String content,
							@RequestParam("tag")String tagName,
							@RequestParam("userId")String userId) {
		Plan plan = new Plan();
		plan.setContent(content);
		plan.setStatus(0);
		System.out.println(tagService.findTagByName(tagName));
		plan.setTag(tagService.findTagByName(tagName));
		plan.setUser(userService.findByUserId(userId));
		plan.setRelease_time(new Timestamp(new Date().getTime()));
		return planService.releasePlan(plan);
	}
	
	/**
	 * 根据id查询plan
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/find")
	public Plan finaPlanById(int id) {
		return planService.findPlanById(id);
	}
	
	@RequestMapping(value = "/start")
	public boolean start_plan(int id) {
		Plan plan = planService.findPlanById(id);
		if(plan != null && plan.getStatus() == 0) {
			plan.setStatus(1);
			planService.save(plan);
			return true;
		}
		return false;
	}
	
	@RequestMapping(value = "/end")
	public boolean end_plan(int id) {
		Plan plan = planService.findPlanById(id);
		if(plan != null && plan.getStatus() == 1) {
			plan.setStatus(2);
			planService.save(plan);
			return true;
		}
		return false;
	}
	
	/**
	 * 根据用户和标签查询当天内的计划
	 * 
	 * @param userId
	 * @param tagId
	 * @return
	 */
	@RequestMapping(value = "/find_by_tag")
	public List<Plan> findPlansByUserAndTag(int userId, int tagId){
		return planService.findPlansByUserAndTag(userId, tagId);
	}
	
	/**
	 * 根据用户和标签查询一段时间内的计划
	 * 
	 * @param userId
	 * @param tagId
	 * @param start
	 * @param end
	 * @return
	 */
	@RequestMapping(value = "/find_by_tag_time")
	public List<Plan> findPlansByUserAndTag(int userId, int tagId, String start, String end){
		Timestamp startTime = null;
		Timestamp endTime = null;
		try {
			startTime = new Timestamp(dateFormat.parse(start).getTime());
			endTime = new Timestamp(dateFormat.parse(end).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<Plan> plans = planService.findPlansByUserAndTag(userId, tagId, startTime, endTime);
		return plans;
	}

}
